package com.ecust.Controller.User;

import com.ecust.Entity.User.User;
import com.ecust.Service.User.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import  java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2017/4/30.
 * @Description 修改个人密码以及个人信息页面跳转
 * @author  mengweiqiang
 */
@Controller
@RequestMapping(value="/user/**")
public class ChangeUserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转到更改个人信息页面
     *
     * @param UserID,model
     * @return 更改个人信息页面字符串，并将UserID传递给下一个页面
     * @author mengweiqiang
     */

    @RequestMapping(value = "ChangePersonInfo")
    public String ChangePersonInfo(@RequestParam String UserID, Model model) {
        User user = userService.getFindUserInfo(UserID).get(0);
        model.addAttribute("user", user);
        return "/ChangePersonInfo";
    }

    /**
     * 跳转到更改用户密码
     *
     * @param UserID,model
     * @return 更改个人信息页面字符串，并将UserID传递给下一个页面
     * @author mengweiqiang
     */
    @RequestMapping(value = "ChangePassword")
    public String ChangePassword(@RequestParam String UserID, Model model) {
        model.addAttribute("UserID", UserID);
        return "/ChangePassword";
    }

    /**
     * 异步检查用户旧密码是否正确
     *
     * @param UserID,UserPwd
     * @return "true or  false"字符串
     * @author mengweiqiang
     */
    @RequestMapping(value = "ajaxCheckOldPassword")
    @ResponseBody
    public String AjaxCheckPassword(@RequestParam String UserID, @RequestParam String UserPwd) {
        boolean flag = userService.getCheckUserPassword(UserID, UserPwd);
        if (flag) {
            System.out.println("true");
            return "true";
        } else {
            System.out.println("false");
            return "false";
        }
    }

    /**
     * 更改用户旧密码
     *
     * @param UserID,NewPassword
     * @return 返回到登录页面
     * @author mengweiqiang
     */
    @RequestMapping(value = "SubmitChangePassword")
    public String SubmitChangePassword(@RequestParam String UserID, @RequestParam String NewPassword, HttpSession session) {
        boolean flag = userService.getChangeUserPassword(UserID, NewPassword);
        if (flag) {
            System.out.println(session.getId());
            session.removeAttribute(session.getId());
            return "redirect:/index.jsp";
        } else {
            return null;
        }
    }

    /**
     * 返回用户个人信息
     *
     * @param UserID,
     * @return 保存用户信息的json对象
     * @author mengweiqiang
     */
    @RequestMapping(value = "AjaxChangePersonInfo")
    @ResponseBody
    public User AjaxChangeUserInfo(@RequestParam String UserID)
    {
        try {
            User user = userService.getFindUserInfo(UserID).get(0);
            return user;
        }catch (Exception e)
        {
            return null;
        }
    }

    /**
     * 更改用户个人信息
     *
     * @param UserID,UseName,Name,Gender,DateOfBirth,TelPhone,Email,Company
     * @return 返回到主界面
     * @author mengweiqiang
     */

    @RequestMapping(value = "ChangePersonInfo",method = RequestMethod.POST)
    @ResponseBody
    public String ChangeUserInfo(String UserID, String UserName, String Name, String Gender, String DateOfBirth, String TelPhone, String Email, String Company, Model model) {
        try {
            System.out.println(DateOfBirth);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pp = new ParsePosition(0);
            System.out.println(sdf.parse(DateOfBirth));
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(DateOfBirth));
            cal.add(Calendar.DATE,1);
            boolean flag = userService.getChangeUserInfo(UserID, UserName, Name, Integer.parseInt(Gender), cal.getTime(), TelPhone, Email, Company);
            System.out.println(Name);
            if (flag) {
                User user = userService.getFindUserInfo(UserID).get(0);
                model.addAttribute("user", user);
                return "/EvaluationHome";
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
